package gov.pbc.gx.nn.pz.dao;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;

import gov.pbc.gx.nn.pz.entity.HcZykbpzyw;
import gov.pbc.gx.nn.pz.entity.HcpzXY;
import gov.pbc.gx.nn.pz.entity.PzFont;

@Repository
public class ZykbpzywDao extends BaseImageDao {

	private Integer PZ_CODE_ = 10;

	public boolean isSameValue(HcZykbpzyw hcZykbpzyw) {
		String type = hcZykbpzyw.getType();
		String pzTemplate = hcZykbpzyw.getPzTemplate();
		String hql = " from HcZykbpzyw where deptId=:deptId and type=:type and pzTemplate=:pzTemplate and invalid=false ";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptId", this.getDeptId());
		map.put("type", type);
		map.put("pzTemplate", pzTemplate);
		List<HcZykbpzyw> hcZykbpzyws = this.query(hql, map);
		if (!hcZykbpzyws.isEmpty()) {
			return true;
		}
		return false;
	}

	public void saveXjpz(HcZykbpzyw hcZykbpzyw) {
		if (!isSameValue(hcZykbpzyw)) {
			hcZykbpzyw.setId(null);
//			hcZykbpzyw.setPzDate(null);
			hcZykbpzyw.setDeptId(this.getDeptId());
			this.save(hcZykbpzyw);
		} else {
			this.update(hcZykbpzyw);
		}
	}

	/*
	 * 根据type，pzTemplate得到实体
	 */
	public HcZykbpzyw selectOneByTypeAndTemplate(String type, String pzTemplate) {
		List<HcZykbpzyw> hcZykbpzyw = null;
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptId", this.getDeptId());
		map.put("pzTemplate", pzTemplate);
		if (type == null) {
			String hql = " from HcZykbpzyw where deptId=:deptId and type is null and pzTemplate=:pzTemplate and  invalid = false";
			hcZykbpzyw = this.query(hql, map);
		} else {
			String hql = " from HcZykbpzyw where deptId=:deptId and type=:type and pzTemplate=:pzTemplate and  invalid = false";
			map.put("type", type);
			hcZykbpzyw = this.query(hql, map);
		}
		if (!hcZykbpzyw.isEmpty()) {
			for (HcZykbpzyw hcZykbpzyw2 : hcZykbpzyw) {
				hcZykbpzyw2.setPzDate(new Date());
			}
			return hcZykbpzyw.get(0);
		}
		return null;
	}

	public Collection<HcZykbpzyw> getCoByType(String type) {
		Collection<HcZykbpzyw> hcZykbpzyw;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptId", this.getDeptId());
		if (type == null) {
			String hql = " from HcZykbpzyw where deptId=:deptId and  invalid = false and type is null";
			hcZykbpzyw = this.query(hql, map);
		} else {
			String hql = " from HcZykbpzyw where deptId=:deptId and invalid = false and type=:type";
			map.put("type", type);
			hcZykbpzyw = this.query(hql, map);
		}
		for (HcZykbpzyw hcZykbpzyw2 : hcZykbpzyw) {
			hcZykbpzyw2.setPzDate(new Date());
		}
		return hcZykbpzyw;
	}

	public Collection<HcZykbpzyw> getOneGroupByType() {
		String hql = " from HcZykbpzyw where deptId=:deptId and invalid = false group by type";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("deptId", this.getDeptId());
		Collection<HcZykbpzyw> hcZykbpzyw = this.query(hql, param);
			for (HcZykbpzyw hcZykbpzyw2 : hcZykbpzyw) {
				hcZykbpzyw2.setPzDate(new Date());
			}
		return hcZykbpzyw;
	}

	public Collection<HcZykbpzyw> getMuByDeptName() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptId", this.getDeptId());
		String hql = "from HcZykbpzyw where deptId=:deptId and invalid=false order by pzTemplate ";
		Collection<HcZykbpzyw> hcZykbpzyw=query(hql, map);
		for (HcZykbpzyw hcZykbpzyw2 : hcZykbpzyw) {
			hcZykbpzyw2.setPzDate(new Date());
		}
		return hcZykbpzyw;
	}

	public List<HcpzXY> getHcPzXYs(Integer pzcode) {
		List<HcpzXY> lists = getHcPzXYsBase(pzcode);
		return lists;
	}

	public void saveXjpzXY(Collection<HcpzXY> hcxjpzxys) {
		saveXjpzXYBase(hcxjpzxys, PZ_CODE_);
	}

	public void updateFont(PzFont pzfont) {
		updateFontBase(pzfont, PZ_CODE_);
	}

	public void deleteTemplateById(String id) {
		this.delete(id, HcZykbpzyw.class);
	}

	public boolean updateTemplateNameDao(final String templatename, final String id) {

		// 得到旧的type
		String sql1 = "from HcZykbpzyw where id=:id and invalid = false ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", id);
		List<HcZykbpzyw> hcZykbpzyw = query(sql1, param);
		String typestr = hcZykbpzyw.get(0).getType();

		String sql = "from HcZykbpzyw where  invalid = false and type=:type ";
		Map<String, Object> param1 = new HashMap<String, Object>();
		param1.put("type", typestr);

		Collection<HcZykbpzyw> hcZykbpzyws = this.query(sql, param1);
		for (HcZykbpzyw zykbpz : hcZykbpzyws) {
			if (zykbpz.getPzTemplate().equals(templatename)) {
				return false;
			}
		}
		return this.doInHibernateSession(new ISessionCallback<Boolean>() {
			String hql = "update HcZykbpzyw set pzTemplate=:pzTemplate  where id=:id ";

			@Override
			public Boolean doInSession(Session session) {
				Query query = session.createQuery(hql);
				query.setString("pzTemplate", templatename);
				query.setString("id", id);
				query.executeUpdate();
				return true;
			}
		});
	}

	public boolean updateTemplateTypeDao(final String templatetype, final String id) {
		// 判斷類型是否有相同的
		String sql = "select count(*) from HcZykbpzyw where type=:type and invalid = false ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("type", templatetype);
		int count = this.queryForInt(sql, param);
		if (count > 0) {
			return false;
		}
		return this.doInHibernateSession(new ISessionCallback<Boolean>() {
			@Override
			public Boolean doInSession(Session session) {

				String sql = "from HcZykbpzyw where id=:id and invalid = false ";
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("id", id);
				List<HcZykbpzyw> zykbpz = query(sql, param);
				String typestr = zykbpz.get(0).getType();

				String sql1 = "from HcZykbpzyw where type=:type and invalid = false ";
				Map<String, Object> param1 = new HashMap<String, Object>();
				param1.put("type", typestr);
				List<HcZykbpzyw> hcZykbpzyw = query(sql1, param1);
				for (HcZykbpzyw zykb : hcZykbpzyw) {
					String hql2 = "update HcZykbpzyw set type=:type  where id=:id ";
					Query query = session.createQuery(hql2);
					query.setString("type", templatetype);
					query.setString("id", zykb.getId());
					query.executeUpdate();
				}

				return true;
			}

		});

	}
	
}
